package com.anjubao.parking.municipal.roadside.task;


import com.anjubao.parking.municipal.basezone.dto.DefaultCityResDto;
import com.anjubao.parking.municipal.common.utils.RedisClientTemplate;
import com.anjubao.parking.municipal.parkinglot.dto.*;
import com.anjubao.parking.municipal.parkinglot.form.AreaScreeningParkingLotForm;
import com.anjubao.parking.municipal.parkinglot.form.ParkingLotSlotScreeningForm;
import com.anjubao.parking.municipal.parkinglot.form.VideoPileQueryForm;
import com.anjubao.parking.municipal.roadside.feign.BaseZoneFeignClient;
import com.anjubao.parking.municipal.roadside.feign.ParkingDeviceFeignClient;
import com.anjubao.parking.municipal.roadside.feign.ParkingLotFeignClient;
import com.anjubao.parking.municipal.roadside.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 路边停车场数据同步定时任务
 *
 * @author anjubao
 */
@Component
public class RecordTask {
    @Autowired
    private ParkingLotFeignClient parkingLotFeignClient;
    @Autowired
    private BaseZoneFeignClient baseZoneFeignClient;
    @Autowired
    private RoadsideParkingLotMapper roadsideParkingLotMapper;
    @Autowired
    private RoadsideParkingLotSlotMapper roadsideParkingLotSlotMapper;
    @Autowired
    private ParkingDeviceFeignClient parkingDeviceFeignClient;
    @Autowired
    private DeviceVideoPileMapper deviceVideoPileMapper;
    @Autowired
    private DeviceStatusMapper deviceStatusMapper;
    @Autowired
    private DeviceVideoPileSlotMapper deviceVideoPileSlotMapper;
    @Autowired
    private DeviceLogMapper deviceLogMapper;
    @Autowired
    private DeviceLogPictureMapper deviceLogPictureMapper;
    @Autowired
    private RedisClientTemplate redisClientTemplate;

    /**
     * logId在reids中的key
     */
    private static final String COMMAND_QUEUE = "ROADSIDE_SERVICE_COMMAND:";

    /**
     * 定时更新停车场数据
     */
    //@Scheduled(cron = "${task.cron}")
    public void refreshParkingLot() {
        /**
         * 获取默认城市信息
         */
        DefaultCityResDto cityResDto = baseZoneFeignClient.getDefaultCity().getResult();
        //查询全部车场信息
        AreaScreeningParkingLotForm parkingLotForm = new AreaScreeningParkingLotForm();
        parkingLotForm.setCityId(cityResDto.getCity().getCityId());
        List<ParkingLotDto> parkingLotDtoList = parkingLotFeignClient.areaScreeningParkingLot(parkingLotForm).getResult();
        //将车场信息同步到路边停车场
        roadsideParkingLotMapper.updateParkingLots(parkingLotDtoList);
    }

    /**
     * 定时更新停车位数据
     */
    //@Scheduled(cron = "${task.cron}")
    public void refreshParkingLotSlot() {
        /**
         * 获取默认城市信息
         */
        DefaultCityResDto cityResDto = baseZoneFeignClient.getDefaultCity().getResult();
        //查询全部泊位信息
        ParkingLotSlotScreeningForm parkingLotSlotForm = new ParkingLotSlotScreeningForm();
        parkingLotSlotForm.setCityId(cityResDto.getCity().getCityId());
        List<ParkingLotSlotDto> parkingLotSlotDtoList = parkingLotFeignClient.getParkingLotSlots(parkingLotSlotForm).getResult();
        //将泊位信息同步到路边停车场
        roadsideParkingLotSlotMapper.updateParkingLotSlot(parkingLotSlotDtoList);
    }

    /**
     * 定时更新设备信息
     */
    //@Scheduled(cron = "${task.cron}")
    public void refreshDeviceMessage() {
        /**
         * 获取默认城市信息
         */
        DefaultCityResDto cityResDto = baseZoneFeignClient.getDefaultCity().getResult();
        //查询全部设备信息
        VideoPileQueryForm videoPileQueryForm = new VideoPileQueryForm();
        videoPileQueryForm.setCityId(cityResDto.getCity().getCityId());
        List<VideoPileDto> videoPileDtoList = parkingDeviceFeignClient.getVideoPile(videoPileQueryForm).getResult();
        //将设备信息同步到路边停车场
        deviceVideoPileMapper.updateDeviceMessage(videoPileDtoList);
        //将设备状态同步到路边停车场
        deviceStatusMapper.updateDeviceStatus(videoPileDtoList);
        //将设备泊位对应信息同步到路边停车场
        for (VideoPileDto videoPileDto : videoPileDtoList) {
            deviceVideoPileSlotMapper.updateDeviceMessageSlot(videoPileDto.getSlots());
        }
    }

    /**
     * 定时推送告警信息到平台
     */
    @Scheduled(cron = "${task.cron}")
    public void refreshDeviceLog() {
        //从reids中获取最新的logId
        String deviceLogId = redisClientTemplate.get(COMMAND_QUEUE+"deviceLogId");
        //查询log信息
        List<DeviceLogDto> deviceLogList=deviceLogMapper.selectDeviceLog(deviceLogId);
        //从reids中获取最新的logPictureId
        String logPictureId = redisClientTemplate.get(COMMAND_QUEUE+"logPictureId");
        //查询告警图片信息
        List<DeviceLogPictureDto> deviceLogPictureList=deviceLogPictureMapper.selectDeviceLogPicture(logPictureId);
        if (!deviceLogList.isEmpty()){
            //将最后一条logId保存到reids中
            redisClientTemplate.set(COMMAND_QUEUE+"deviceLogId",deviceLogList.get(deviceLogList.size()-1).getId());
            //将log信息同步到平台
            parkingDeviceFeignClient.saveLog(deviceLogList);
        }
        if (!deviceLogPictureList.isEmpty()){
            //将最后一条logPictureId保存到reids中
            redisClientTemplate.set(COMMAND_QUEUE+"logPictureId",deviceLogPictureList.get(deviceLogPictureList.size()-1).getId());
            //将告警图片信息同步到平台
            parkingDeviceFeignClient.savePicture(deviceLogPictureList);
        }
    }

    /**
     * 定时更新车场车位信息到平台
     */
    @Scheduled(cron = "${task.cron}")
    public void refreshFreeSlots() {
        //查询空闲车位信息
        List<ParkingLotStatusDto> parkingLotStatusDtoList=roadsideParkingLotSlotMapper.selectFreeSlots();
        //将数据更新到平台
        parkingLotFeignClient.updateParkingLotFreeList(parkingLotStatusDtoList);
    }


}
